package com.mx.app.swing;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class SwingDemo {

    public static void showPage(){
        SwingUtilities.invokeLater(SwingDemo::createAndShowGUI);
    }
    private static void createAndShowGUI() {
        // 创建主窗口
        JFrame mainFrame = new JFrame("随机数据列表");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(800, 400);
        mainFrame.setLayout(new BorderLayout());

        // 定义表格列名
        String[] columnNames = {"工号", "姓名", "集团编码", "工号ID", "网点名称", "销售点编码"};

        // 创建表格数据模型
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);

        // 创建表格
        JTable table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);

        // 添加表格到主窗口
        mainFrame.add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();

        // 添加生成数据按钮
        JButton generateButton = new JButton("生成数据");
        buttonPanel.add(generateButton);

        // 添加导出数据按钮
        JButton exportButton = new JButton("导出数据");
        buttonPanel.add(exportButton);

        mainFrame.add(buttonPanel, BorderLayout.SOUTH);

        // 生成随机数据的事件处理
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 清空现有数据
                tableModel.setRowCount(0);

                // 随机生成 10 行数据
                for (int i = 0; i < 1000; i++) {
                    String employeeId = "E" + (1000 + new Random().nextInt(9000));
                    String name = "姓名" + (i + 1);
                    String groupCode = "G" + (100 + new Random().nextInt(900));
                    String employeeCode = "ID" + (1000 + new Random().nextInt(9000));
                    String branchName = "网点" + (i + 1);
                    String salesPointCode = "SP" + (100 + new Random().nextInt(900));

                    // 添加一行到表格
                    tableModel.addRow(new Object[]{employeeId, name, groupCode, employeeCode, branchName, salesPointCode});
                }
            }
        });

        // 导出数据到 CSV 文件的事件处理
        exportButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle("保存为");

                // 添加 .csv 作为默认扩展名
                fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("CSV 文件 (*.csv)", "csv"));

                int userSelection = fileChooser.showSaveDialog(mainFrame);

                if (userSelection == JFileChooser.APPROVE_OPTION) {
                    try (FileWriter writer = new FileWriter(fileChooser.getSelectedFile() + ".csv")) {
                        // 写入表头
                        for (int i = 0; i < tableModel.getColumnCount(); i++) {
                            writer.write(tableModel.getColumnName(i) + (i < tableModel.getColumnCount() - 1 ? "," : "\n"));
                        }

                        // 写入数据
                        for (int i = 0; i < tableModel.getRowCount(); i++) {
                            for (int j = 0; j < tableModel.getColumnCount(); j++) {
                                writer.write(tableModel.getValueAt(i, j).toString() + (j < tableModel.getColumnCount() - 1 ? "," : "\n"));
                            }
                        }

                        JOptionPane.showMessageDialog(mainFrame, "数据导出成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(mainFrame, "导出失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });

        // 显示窗口
        mainFrame.setLocationRelativeTo(null); // 居中显示
        mainFrame.setVisible(true);
    }
}
